import pymysql
import urllib.request
import json
from datetime import date, datetime, timedelta

db = pymysql.connect("114.115.160.16", "root", "123456Aa!", "fund")
cursor = db.cursor()

headers = {
    "token": "atTPd9c8sA"
}
sql = "select fund_id from api_fund"
cursor.execute(sql)
funds = []
for fund_id in cursor.fetchall():
    funds.append(fund_id)

response = urllib.request.urlopen("https://api.doctorxiong.club/v1/fund/hot" + "?token=atTPd9c8sA").read().decode(
    "UTF-8")
data = json.loads(response)
data = data["data"]["rank"]

popular_id = 1

for fund in data:
    if fund["code"] in funds:
        sql = "insert into api_popular values(%d,'%s','%s')" % (
        popular_id, datetime.now().__format__("%Y-%m-%d"), fund["code"])
        popular_id += 1
        cursor.execute(sql)
        db.commit()

